
import { Toast } from 'antd-mobile'
import {reqinfo,reqcartadd} from '../../request/api'
import { getuserinfo } from './login'
//初始状态
const initState={
        info:{},//商品详情信息
       
}

//TYPES
const TYPES={
        CHANGE_GOODSINFO:'CHANGE_GOODSINFO',
        
}

//reducer
const reducer =(state=initState,action)=>{
        switch(action.type){
                case TYPES.CHANGE_GOODSINFO:
                        return{
                                ...state,
                                info:action.info
                        }
                default:
                        return state
        }
}

//creators 做逻辑 方法  请求
export const actions={
        changeGInfo:info=>({type:TYPES.CHANGE_GOODSINFO,info}),
        //商品详情请求
        requestGInfo:(id)=>(dispatch,getState)=>{
                reqinfo({id}).then(res=>{
                        let info=res.data.list[0];
                        dispatch(actions.changeGInfo(info))
                })
        },
        // 加入购物车
        addcart:()=>(dispatch,getState)=>{
          let uid =getuserinfo(getState()).uid
          let goodsid=getGInfo(getState()).id
          let type=goodsid?1:2
          reqcartadd({
                  uid,type,goodsid,num:1
          }).then(res=>{
                  if(res.data.code==200){
                          Toast.show({
                                  content:res.data.msg
                          })
                  }
          })
        }
}

// 导出数据给组件
export const getGInfo=state=>state.detail.info
// 导出reducer
export default reducer